const router = require('koa-router')()

router.prefix('/flowerpot')

//backend   所有花盆
//页面初始化时获取数据库内的数据条目
router.post('/getpot', async (ctx, next) => {
    let sql = 'SELECT * FROM flowerpot ORDER BY potid'
    let result = await ctx.db.EXCUTE(sql);
    ctx.body = {
        code: 200,
        data: result
    };
})
router.get('/getpots', async (ctx, next) => {
    let sql = 'SELECT * FROM flowerpot ORDER BY potid'
    let result = await ctx.db.EXCUTE(sql);
    console.log(result);
    ctx.body = {
        code: 200,
        data: result
    };
})

router.post('/getmypot', async (ctx, next) => {
    let potid = ctx.request.body.potid
    console.log(potid);
    let sql = 'SELECT * FROM flowerpot where potid=?'
    let result = await ctx.db.EXCUTE(sql, [potid]);
    // console.log(result);
    ctx.body = {
        code: 200,
        data: result
    };
})

router.post('/modifyuuploadflowerpotimgbackend', async (ctx, next) => {
    let potid = ctx.request.body.potid;
    let potname = ctx.request.body.potname;
    let potimg = ctx.request.body.potimg;

    let potimglist = await ctx.db.EXCUTE("select potimg from flowerpot where potid=?", [potid]);
    console.log(potimglist[0].potimg);
    if (potimglist[0].potimg == potimg) {
        for (let i = 0; i < potimg.length; i++) {
            let m = potimg[i].replace('/http://localhost:8080/', '')
            let n = potimg[i].split('http://localhost:8080/')[1]
            console.log(m);
            console.log(n);
            fs.unlink(path.join(__dirname, '../' + n), async function (err) {
                if (err) {
                    console.log(err);
                    ctx.body = {
                        code: 400
                    }
                }
                await ctx.db.EXCUTE("update flowerpot set potimg='' where potid=?", [potid]);
            })
        }
    }

    for (let i = 0; i < ctx.request.body.potimg.length; i++) {
        console.log('potimg[i]');
        console.log(potimg[i])
        if (!potimg[i].url.includes("http://localhost:8080")) {
            var base64Data = potimg[i].url.replace(/^data:image\/\w+;base64,/, "");
            var dataBuffer = Buffer.from(base64Data, 'base64');
            let imgUrl = uploadDir + '\\' + potimg[i].name
            fs.writeFileSync(imgUrl, dataBuffer)

            if (i == 0) {
                console.log(potid);
                console.log('/images/pot/' + potimg[i].name);
            }
            let sql = 'UPDATE flowerpot SET potimg=?, potname=? WHERE potid=?'
            await ctx.db.EXCUTE(sql, ['/images/pot/' + potimg[i].name, potname, potid]);
        } else {
            if (i == 0) {
                console.log(potimg[0]);
                let n = potimg[i].url.split('http://localhost:8080')[1]
                console.log(n);
                let sql = 'UPDATE flowerpot SET potimg=?, potname=? WHERE potid=?'
                await ctx.db.EXCUTE(sql, ['/images/pot/' + potimg[i].name, potname, potid]);
            }
        }
    }

    let sql = 'SELECT * FROM flowerpot ORDER BY potid'
    let result = await ctx.db.EXCUTE(sql);
    ctx.body = {
        code: 200,
        data: result
    };

});

//编辑item 首先获取该项的内容
router.post('/getsingle', async (ctx, next) => {
    let potid = ctx.request.body.potid;
    console.log(potid);
    let sql = 'SELECT * FROM flowerpot WHERE potid=?';
    let result = await ctx.db.EXCUTE(sql, [potid]);
    console.log(result);
    ctx.body = {
        code: 200,
        data: result
    }
})
router.post('/getsingle1', async (ctx, next) => {
    let potid = ctx.request.body.potid;
    let userid = ctx.request.body.userid;
    console.log(potid);
    console.log(userid);
    console.log(typeof (potid));
    let sql = 'SELECT * FROM flowerpot WHERE potid=?';
    let result = await ctx.db.EXCUTE(sql, [potid]);

    let sql1 = 'SELECT * FROM userflowerpot WHERE userid=?';
    let result1 = await ctx.db.EXCUTE(sql1, [userid]);
    console.log(result1);
    console.log(result1.length);
    for (let i = 0; i < result1.length; i++) {
        console.log(result1[i].potid);
        console.log(typeof (result1[i].potid));
        console.log(result1[i].potid == potid);
        if (result1[i].potid == potid) {
            ctx.body = {
                code: 200,
                data: result,
                text: "立即使用"
            }
            return 
        } else if (i == result1.length - 1) {
            ctx.body = {
                code: 200,
                data: result,
                text: "立即兑换"
            }
        }
    }
    console.log(result);
})

// 用户兑换花盆
router.post('/exchange', async (ctx, next) => {
    let potid = ctx.request.body.potid;
    let date = ctx.request.body.date;
    let userid = ctx.request.body.userid;
    console.log(potid);
    
    let sql = 'insert into userflowerpot (userid, potid) values (?, ?)';
    let result = await ctx.db.EXCUTE(sql, [userid, potid]);
    let result2 = await ctx.db.EXCUTE("select * from coin where userid=? order by date desc", [userid])

    let result0 = await ctx.db.EXCUTE("insert into coin (userid, addcoin, date, sumcoin, type) values (?, 200, ?, ?, 2) ", [userid, date, result2[0].sumcoin-200]);
    console.log(result);
    ctx.body = {
        code: 200,
        data: result
    }
})

router.post('/usepot', async (ctx, next) => {
    let potid = ctx.request.body.potid;
    let userid = ctx.request.body.userid;
    console.log(potid);
    let sql = 'update user set potid=? where userid=?';
    let result = await ctx.db.EXCUTE(sql, [potid, userid]);
    console.log(result);
    ctx.body = {
        code: 200,
        data: result
    }
})

module.exports = router